package com.github.dice.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.dice.service.IPlayerService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.dice.entity.Player;
import com.github.dice.entity.RoomAndPlayerMapping;
import com.github.dice.mapper.RoomAndPlayerMappingMapper;
import com.github.dice.service.IRoomAndPlayerMappingService;

@Service
public class RoomAndPlayerMappingService extends ServiceImpl<RoomAndPlayerMappingMapper, RoomAndPlayerMapping>
        implements IRoomAndPlayerMappingService {

    @Resource
    private IPlayerService             playerService;

    @Resource
    private RoomAndPlayerMappingMapper roomAndPlayerMappingMapper;

    public RoomAndPlayerMapping addRoomAndPlayerMapping(RoomAndPlayerMapping roomAndPlayerMapping) {
        RoomAndPlayerMapping result = getRoomAndPlayerMapping(roomAndPlayerMapping.getRoomNumber(),
                roomAndPlayerMapping.getPlayerName());
        if (ObjectUtils.isEmpty(result)) {
            roomAndPlayerMapping.setIsDelete(0);
            roomAndPlayerMappingMapper.insert(roomAndPlayerMapping);
            return roomAndPlayerMapping;
        } else {
            return result;
        }
    }

    public RoomAndPlayerMapping getRoomAndPlayerMapping(@Nonnull String roomNumber, @Nonnull String playerName) {
        QueryWrapper<RoomAndPlayerMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_number", roomNumber);
        queryWrapper.eq("player_name", playerName);
        queryWrapper.eq("is_delete", 0);
        return roomAndPlayerMappingMapper.selectOne(queryWrapper);
    }

    public List<Player> getRoomAndPlayerMappingListByRoomNumber(@Nonnull String roomNumber) {
        List<Player> players = new ArrayList<>();
        QueryWrapper<RoomAndPlayerMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_number", roomNumber);
        queryWrapper.eq("is_delete", 0);
        List<RoomAndPlayerMapping> roomAndPlayerMappings = roomAndPlayerMappingMapper.selectList(queryWrapper);
        roomAndPlayerMappings.forEach(roomAndPlayerMapping -> {
            Player player = playerService.getPlayerByName(roomAndPlayerMapping.getPlayerName());
            if (!ObjectUtils.isEmpty(player)) {
                players.add(player);
            }
        });
        return players;
    }

    @Override
    public void delPlayerMappingByRoom(String roomNumber) {
        UpdateWrapper<RoomAndPlayerMapping> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("room_number",roomNumber);
        RoomAndPlayerMapping roomAndPlayerMapping = new RoomAndPlayerMapping();
        roomAndPlayerMapping.setIsDelete(0);
        roomAndPlayerMappingMapper.update(roomAndPlayerMapping,updateWrapper);
    }
}
